{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "x_input = np.array([\n",
    "        [1,1,1,0,0],\n",
    "        [0,1,1,1,0],\n",
    "        [0,0,1,1,1],\n",
    "        [0,0,1,1,0],\n",
    "        [0,1,1,0,0]\n",
    "], dtype=np.float32)\n",
    "\n",
    "x_kernel_1 = np.array([\n",
    "    [1,0,1],\n",
    "    [0,1,0],\n",
    "    [1,0,1]\n",
    "], dtype=np.float32)\n",
    "\n",
    "\n",
    "tf_x_input    = tf.constant(np.reshape(x_input,    newshape=[1,5,5,1]))\n",
    "tf_x_kernel_1 = tf.constant(np.reshape(x_kernel_1, newshape=[3,3,1,1]))\n",
    "\n",
    "y1 = tf.nn.conv2d(tf_x_input, tf_x_kernel_1, strides=[1,1,1,1], padding=\"VALID\")\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    [y1_cov] = sess.run([y1])\n",
    "\n",
    "y1_cov[0,:,:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 考虑第二种卷积核的输出 y2_cov\n",
    "x_kernel_2 = np.array([\n",
    "    [0,1,0],\n",
    "    [1,0,1],\n",
    "    [0,1,0]\n",
    "], dtype=np.float32)\n",
    "\n",
    "tf_x_kernel_2 = tf.constant(np.reshape(x_kernel_2, newshape=[3,3,1,1]))\n",
    "y2 = tf.nn.conv2d(tf_x_input, tf_x_kernel_2, strides=[1,1,1,1], padding=\"VALID\")\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    [y1_cov,y2_cov] = sess.run([y1, y2])\n",
    "\n",
    "\n",
    "print(u\"第一种卷积核扫描结果：\")\n",
    "print(y1_cov[0,:,:,0])\n",
    "print(u\"第二种卷积核扫描结果：\")\n",
    "print(y2_cov[0,:,:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# 输入层加一个图层，里面内容同之前，这样输入维度增加到2\n",
    "x_input2 = np.zeros([1,5,5,2])\n",
    "x_input2[0,:,:,0] = x_input\n",
    "x_input2[0,:,:,1] = x_input\n",
    "\n",
    "\n",
    "# 输入维度增加到2，卷积层输入维度同样做相应的增加，但这里输出仍然是一个维度\n",
    "x_kernel_3 = np.zeros([3,3,2,1])\n",
    "x_kernel_3[:,:,0,0] = x_kernel_1\n",
    "x_kernel_3[:,:,1,0] = x_kernel_2\n",
    "\n",
    "tf_x_input2  = tf.constant(x_input2.astype(np.float32) )\n",
    "tf_x_kernel_3 = tf.constant(x_kernel_3.astype(np.float32) )\n",
    "\n",
    "y3 = tf.nn.conv2d(tf_x_input2, tf_x_kernel_3, strides=[1,1,1,1], padding=\"VALID\")\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    [y3_cov] = sess.run([y3])\n",
    "\n",
    "# 发现这里卷积层输出内容，是单独两个卷积核扫描结果的直接相加\n",
    "print(u\"第一、第二种卷积核扫描结果简单相加：\")\n",
    "print((y1_cov+y2_cov)[0,:,:,0])\n",
    "\n",
    "print(u\"第一、第二种卷积核组合后扫描两层相同输入图层结果结果：\")\n",
    "print(y3_cov[0,:,:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_kernel_4 = np.zeros([3,3,2,2])\n",
    "x_kernel_4[:,:,0,0] = x_kernel_1\n",
    "x_kernel_4[:,:,1,0] = x_kernel_1\n",
    "x_kernel_4[:,:,0,1] = x_kernel_2\n",
    "x_kernel_4[:,:,1,1] = x_kernel_2\n",
    "\n",
    "x_kernel_5 = np.zeros([3,3,2,2])\n",
    "x_kernel_5[:,:,0,0] = x_kernel_1\n",
    "x_kernel_5[:,:,0,1] = x_kernel_1\n",
    "x_kernel_5[:,:,1,0] = x_kernel_2\n",
    "x_kernel_5[:,:,1,1] = x_kernel_2\n",
    "\n",
    "tf_x_kernel_4 = tf.constant(x_kernel_4.astype(np.float32))\n",
    "tf_x_kernel_5 = tf.constant(x_kernel_5.astype(np.float32))\n",
    "y4 = tf.nn.conv2d(tf_x_input2, tf_x_kernel_4, strides=[1,1,1,1], padding=\"VALID\")\n",
    "y5 = tf.nn.conv2d(tf_x_input2, tf_x_kernel_5, strides=[1,1,1,1], padding=\"VALID\")\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    [y4_cov, y5_cov] = sess.run([y4, y5])\n",
    "    \n",
    "print(u\"输出层用相同卷积图层的结果\")\n",
    "print(y4_cov[0,:,:,0])\n",
    "print(y4_cov[0,:,:,1])\n",
    "\n",
    "print(u\"输入层用相同卷积图层的结果\")\n",
    "print(y5_cov[0,:,:,0])\n",
    "print(y5_cov[0,:,:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "x_kernel_3 = np.array([\n",
    "    [1,1,1],\n",
    "    [1,1,1],\n",
    "    [1,1,1]\n",
    "], dtype=np.float32)\n",
    "tf_x_kernel_3 = tf.constant(np.reshape(x_kernel_3, newshape=[3,3,1,1]))\n",
    "\n",
    "y1_trans = tf.nn.conv2d_transpose(y1, tf_x_kernel_3, output_shape=[1,5,5,1], strides=[1,2,2,1], padding=\"SAME\")\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    [y1_cov_tran] = sess.run([y1_trans])\n",
    "\n",
    "print(u\"反卷积输入\")\n",
    "print(y1_cov[0,:,:,0])    \n",
    "print(u\"反卷积输出\")\n",
    "print(y1_cov_tran[0,:,:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y1_trans = tf.nn.conv2d_transpose(y1, tf_x_kernel_3, output_shape=[1,9,9,1], strides=[1,4,4,1], padding=\"SAME\")\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    [y1_cov_tran] = sess.run([y1_trans])\n",
    "\n",
    "print(u\"反卷积输入\")\n",
    "print(y1_cov[0,:,:,0])    \n",
    "print(u\"反卷积输出\")\n",
    "print(y1_cov_tran[0,:,:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_input_tran_zero = np.array([\n",
    "        [4,0,3,0,4],\n",
    "        [0,0,0,0,0],\n",
    "        [2,0,4,0,3],\n",
    "        [0,0,0,0,0],\n",
    "        [2,0,3,0,4]\n",
    "], dtype=np.float32)\n",
    "tf_input_tran_zero    = tf.constant(np.reshape(x_input_tran_zero, newshape=[1,5,5,1]))\n",
    "y1_trans_zero = tf.nn.conv2d(tf_input_tran_zero, tf_x_kernel_3, strides=[1,1,1,1], padding=\"SAME\")\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    [y1_cov_tran2] = sess.run([y1_trans_zero])\n",
    "    \n",
    "print(u\"反卷积输出\")\n",
    "print(y1_cov_tran2[0,:,:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
